package com.wangjie.normal;

import java.util.Arrays;

/**
 * 对猫进行排序
 */
public class CatSort {
    static class Cat {
        private int weight, height;

        public Cat(int weight, int height) {
            this.weight = weight;
            this.height = height;
        }

        public int compareTo(Cat c) {
            if (this.weight < c.weight) return -1;
            else if (this.weight > c.weight) return 1;
            else return 0;
        }

        @Override
        public String toString() {
            return "Cat{" +
                    "weight=" + weight +
                    ", height=" + height +
                    '}';
        }
    }

    public static void sort(Cat[] cats) {
        for (int i = 0; i < cats.length; i++) {
            int minPos = i;
            for (int j = i + 1; j < cats.length; j++) {
                minPos = (cats[minPos].compareTo(cats[j]) == -1) ? minPos : j;
            }
            if(minPos != i){
                swap(cats,i,minPos);
            }
        }
    }

    private static void swap(Cat[] cats, int i, int minPos) {
        Cat temp = cats[i];
        cats[i] = cats[minPos];
        cats[minPos] = temp;
    }

    public static void main(String[] args) {
        Cat[] cats = {new Cat(1,1),new Cat(5,5),new Cat(3,3)};
        sort(cats);
        System.out.println(Arrays.toString(cats));
    }
}
